package com.myway.core.jersey.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.zip.GZIPInputStream;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

/**
 * 类名称：ElongClient
 * 类描述：艺龙http接口调用
 * 创建时间：2016-3-9 下午6:26:15
 * @author jiangyang
 * @version V1.0
 */
public class ElongClient {

	public String request(String method, String url, String data) throws Exception {
		HttpURLConnection conn = null;
		InputStream in = null;
		InputStreamReader isr = null;
		OutputStream out = null;
		StringBuffer result=new StringBuffer();
		try {
			URL u = new URL(url);
			conn = (HttpURLConnection) u.openConnection();
			if(conn instanceof HttpsURLConnection) {  
	               /* SSLContext sc = SSLContext.getInstance("SSL");  
	                sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());  
	                ((HttpsURLConnection) conn).setSSLSocketFactory(sc.getSocketFactory()); */ 
	                ((HttpsURLConnection) conn).setHostnameVerifier(new TrustAnyHostnameVerifier());  
	        } 
			conn.setRequestProperty("Accept-Encoding", "gzip");
			// conn.setRequestProperty("Content-Type", "");
			conn.setRequestProperty("Connection", "keep-alive");

			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setConnectTimeout(60000);
			conn.setReadTimeout(60000);

			if (method.equals("POST")) {
				byte[] sendbyte = data.getBytes("UTF-8");
				out = conn.getOutputStream();
				out.write(sendbyte);
			}

			int status = conn.getResponseCode();
			if (status == 200) {
				String enc = conn.getContentEncoding();
				result = new StringBuffer();
				in = conn.getInputStream();
				enc = conn.getContentEncoding();
				if (enc != null && enc.equals("gzip")) {
					GZIPInputStream gzin = new GZIPInputStream(in);
					isr = new InputStreamReader(gzin, "UTF-8");
				} else {
					isr = new InputStreamReader(in, "UTF-8");
				}
				char[] c = new char[1024];
				int a = isr.read(c);
				while (a != -1) {
					result.append(new String(c, 0, a));
					a = isr.read(c);
				}
			} else {
				result.append("return code is ");
				result.append(status);
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
			try {
				if (in != null) {
					in.close();
				}
				if (isr != null) {
					isr.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result == null ? null : result + "";
	}
	  public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException  
      {  
      }  

      public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException  
      {  
      }  

      public X509Certificate[] getAcceptedIssuers()  
      {  
          return new X509Certificate[]{};  
      }  

      private static class TrustAnyHostnameVerifier implements HostnameVerifier {  
	      public boolean verify(String hostname, SSLSession session)  
	      {  
	          return true;  
	      }  
      }  
}
